package com.namecard.namecardapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.constants.ConstantsClass;
import com.namecard.namecardapi.entity.Country;
import com.namecard.namecardapi.entity.vo.CountryVO;
import com.namecard.namecardapi.mapper.CountryMapper;
import com.namecard.namecardapi.service.CountryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
@DS("sharding")
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements CountryService {
    @Override
    public List<Country> getByCountryName(String countryName) {
        LambdaQueryWrapper<Country> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(countryName), Country::getName, countryName);
        return this.list(queryWrapper);
    }

    @Override
    public void mySave(Country country) {
        boolean exist = queryByName(country);
        if (!exist) {
            this.save(country);
        }
    }

    public boolean queryByName(Country country) {
        LambdaQueryWrapper<Country> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Country::getName, country.getName());
        return this.count(queryWrapper) > 0;
    }

    @Override
    public List<Country> selectCountryList() {
        LambdaQueryWrapper<Country> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Country::getType, ConstantsClass.COUNTRY);
        return this.list();
    }

    @Override
    public List<Country> getProvinceByCountryName(String countryName) {
        if (StringUtils.isNotBlank(countryName)) {
            return baseMapper.getProvinceByCountryName(countryName);
        }
        return null;
    }

    @Override
    public List<Country> getProvinceByCountryId(Integer countryId) {
        if (countryId != null) {
            return baseMapper.getProvinceByCountryId(countryId);
        }
        return null;
    }

    @Override
    public List<CountryVO> getTree() {
        // 查询所有国家和省份数据
        List<Country> countries = baseMapper.selectList(null);
        // 将所有国家转换为CountryVO

        return countries.stream()
                .filter(c -> c.getParentId() == null) // 过滤出顶层的国家
                .map(c -> convertToCountryVO(c, countries))
                .collect(Collectors.toList());
    }

    private CountryVO convertToCountryVO(Country country, List<Country> allCountries) {
        CountryVO countryVO = new CountryVO();
        BeanUtils.copyProperties(country, countryVO);

        // 找出子级省份并递归
        List<CountryVO> children = allCountries.stream()
                .filter(c -> c.getParentId() != null && c.getParentId().equals(country.getId()))
                .map(c -> convertToCountryVO(c, allCountries))
                .collect(Collectors.toList());

        countryVO.setChildren(children);

        return countryVO;
    }
}

